/**********************************************************************************
* CLASS
*		RWxml
*
* DESCRIPTION
* 		Read and Write xml file
*
* REVISION HISTORY
*		Revision 1.0 2008/12/10 Atthaboon S.
*		First Release
*
**********************************************************************************/
var RWXml = {

	/****************************************************
	* FUNCTION 
	*		loadXML
	*
	* DESCRIPTION
	* 		Create Dom Book Page.
	*
	* ARGUMENTS
	*		bookXML		:	String of xml Book Page.
	*		pageGenesis		:	Page Genesis Object
	*
	****************************************************/
	loadXML : function(bookXML,pageGenesis)
	{
		var genesisContainer = new Array();
		//Load XML string into DOM Object
		try //Internet Explorer
	    {
			xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async="false";
			xmlDoc.loadXML(bookXML);
		}
		catch(e)
		{
			try //Firefox, Mozilla, Opera, etc.
			{
				parser=new DOMParser();
				xmlDoc=parser.parseFromString(bookXML,"text/xml");
			}
			catch(e) {alert(e.message)}
		}
		
		try 
		{
			// Get the root node (BookPage)
			var domPage = xmlDoc.getElementsByTagName('BookPage')[0];
			alert(domPage.nodeName+' '+domPage.childNodes.length);
						
			for (var i =0; i<domPage.childNodes.length; i++) 
			{
				var node = domPage.childNodes[i];
				if('BookImage' == node.nodeName)
				{
					alert('Load BookImage');
					var genesisObj = this.loadBookImage(node);
					genesisContainer[genesisContainer.length] = genesisObj; 
					pageGenesis.appendChild(genesisObj.getDomObj());
				}
				else if('Content' == node.nodeName)
				{
					alert('Load Content');
					var genesisObj = this.loadBookContent(node)
					genesisContainer[genesisContainer.length] = genesisObj; 
					pageGenesis.appendChild(genesisObj.getDomObj());
				}
				else if('ExternalEmbeded' == node.nodeName)
				{
					alert('Load Genesis');
					var genesisObj = this.loadBookExternalEmbeded(node);
					genesisContainer[genesisContainer.length] = genesisObj;
					pageGenesis.appendChild(genesisObj.getDomObj());
				}
				else if('Genesis' == node.nodeName)
				{
					alert('Load Genesis');
					var genesisObj = this.loadBookGenesis(node);
					genesisContainer[genesisContainer.length] = genesisObj;
					pageGenesis.appendChild(genesisObj.getDomObj());
				}
			}
		}
		catch(e) 
		{
			alert(e.message)
		}
		
		// Return the Array of Genesis Object
		return genesisContainer;
	},
	
	/****************************************************
	* FUNCTION 
	*		loadBookImage
	*
	* DESCRIPTION
	* 		Create Book Image from xml Dom.
	*
	* ARGUMENTS
	*		bookDom	:	BookImage Dom
	*
	* RETURN
	*		BookImageObject
	*
	****************************************************/
	loadBookImage : function(bookDom)
	{
		var style		= bookDom.getElementsByTagName('PropertyStyle')[0];
		var bookimage  	= new BookImage(bookDom.getElementsByTagName('src')[0].getAttribute('value'),style.getElementsByTagName('left')[0].getAttribute('value'),style.getElementsByTagName('top')[0].getAttribute('value'));
		bookimage.setSize(style.getElementsByTagName('width')[0].getAttribute('value'),style.getElementsByTagName('height')[0].getAttribute('value'));
		bookimage.fade(style.getElementsByTagName('opacity')[0].getAttribute('value'),style.getElementsByTagName('opacity')[0].getAttribute('value'),0);
		return bookimage;
	},
	
	/****************************************************
	* FUNCTION 
	*		loadBookContent
	*
	* DESCRIPTION
	* 		Create Book Content from xml Dom.
	*
	* ARGUMENTS
	*		bookDom	:	BookImage Dom
	*
	* RETURN
	*		ContentObject
	*
	****************************************************/
	loadBookContent : function(bookDom)
	{
		var style		= bookDom.getElementsByTagName('PropertyStyle')[0];
		var content  	= new Content(style.getElementsByTagName('left')[0].getAttribute('value'),style.getElementsByTagName('top')[0].getAttribute('value'));
		content.setSize(style.getElementsByTagName('width')[0].getAttribute('value'),style.getElementsByTagName('height')[0].getAttribute('value'));
		content.setText(bookDom.getElementsByTagName('innerHTML')[0].firstChild.data);
		return content;
	},
	
	/****************************************************
	* FUNCTION 
	*		loadBookExternalEmbeded
	*
	* DESCRIPTION
	* 		Create Book External Embeded from xml Dom.
	*
	* ARGUMENTS
	*		bookDom	:	BookGenesis Dom
	*
	* RETURN
	*		externalEmbededObject
	*
	****************************************************/
	loadBookExternalEmbeded : function(bookDom)
	{
		var style 		    		= bookDom.getElementsByTagName('PropertyStyle')[0];
		var externalEmbededObject	= new ExternalEmbeded(style.getElementsByTagName('left')[0].getAttribute('value'),style.getElementsByTagName('top')[0].getAttribute('value'),bookDom.getElementsByTagName('innerHTML')[0].firstChild.data);
		externalEmbededObject.setSize(style.getElementsByTagName('width')[0].getAttribute('value'),style.getElementsByTagName('height')[0].getAttribute('value'));
		return externalEmbededObject;
	},
	
	/****************************************************
	* FUNCTION 
	*		loadBookGenesis
	*
	* DESCRIPTION
	* 		Create Book Genesis from xml Dom.
	*
	* ARGUMENTS
	*		bookDom	:	BookGenesis Dom
	*
	* RETURN
	*		GenesisObject
	*
	****************************************************/
	loadBookGenesis : function(bookDom)
	{
		var style 		    = bookDom.getElementsByTagName('PropertyStyle')[0];
		var bookgenesis  	= new Genesis(style.getElementsByTagName('left')[0].getAttribute('value'),style.getElementsByTagName('top')[0].getAttribute('value'));
		bookgenesis.setSize(style.getElementsByTagName('width')[0].getAttribute('value'),style.getElementsByTagName('height')[0].getAttribute('value'));
		var childnode		= bookDom.getElementsByTagName('childnode');
		for (var i =0; i<childnode.length; i++) 
		{
			var node	= childnode[i];
			if('BookImage' == node.nodeName)
			{
				var bookimage = this.loadBookImage(node);
				bookgenesis.appendChild(bookimage.getDomObj());
			}
			else if('Content' == node.nodeName)
			{
				var content   = this.loadBookContent(node);
				bookgenesis.appendChild(content.getDomObj());
			}
			else if('ExternalEmbeded' == node.nodeName)
			{
				var externalEmbeded	=	this.loadBookExternalEmbeded(node);
				bookgenesis.appendChild(externalEmbeded.getDomObj());
			}
		}
		return bookgenesis;
	},
	
	/****************************************************
	* FUNCTION 
	*		saveXML
	*
	* DESCRIPTION
	* 		Create Dom Book Page.
	*
	* ARGUMENTS
	*		container		:	Genesis Object container
	*		index			:	Book Page index
	*
	****************************************************/
	saveXML : function(container,index)
	{
		// initial strXML string
		var strXML   = '<?xml version="1.0" encoding="WINDOWS-874"?>';
		var root	 = new Element('Root',{});
		var bookpage = new Element('BookPage',{'value':index});
		root.appendChild(bookpage);
		
		for (var i = 0;i<container.length;i++)
		{
			var genesisObj = container[i];
			if("BookImage" == genesisObj.getDomType())
			{
				bookpage.appendChild(this.saveBookImage(genesisObj.getDomObj()));
			}
			else if("Content" == genesisObj.getDomType())
			{
				bookpage.appendChild(this.saveBookContent(genesisObj.getDomObj()));
			}
			else if('ExternalEmbeded' == genesisObj.getDomType())
			{
				bookpage.appendChild(this.saveBookExternalEmbeded(genesisObj.getDomObj()));
			}
			else if("Genesis" == genesisObj.getDomType())
			{
				bookpage.appendChild(this.saveBookGenesis(genesisObj.getDomObj()));
			}
		}
		alert(strXML+''+root.innerHTML);
		
		// Replace node name
		return strXML+''+root.innerHTML;
	},
	
	/****************************************************
	* FUNCTION 
	*		svaeBookImage
	*
	* DESCRIPTION
	* 		Create String Book Image from Dom.
	*
	* ARGUMENTS
	*		bookDom	:	BookImage Dom
	*
	* RETURN
	*		bookxmlDom
	*
	****************************************************/
	saveBookImage : function(bookDom)
	{
		var genesisObj = new Element("BookImage",{});
		var src		   = new Element("src",{'value':bookDom.src});
		genesisObj.appendChild(src);
		var style      = new Element('PropertyStyle',{});
		genesisObj.appendChild(style);
		style.appendChild(new Element("left"   ,{'value':bookDom.style.left}));
		style.appendChild(new Element("top"    ,{'value':bookDom.style.top}));
		style.appendChild(new Element("width"  ,{'value':bookDom.style.width}));
		style.appendChild(new Element("height" ,{'value':bookDom.style.height}));
		style.appendChild(new Element("opacity",{'value':bookDom.getStyle('opacity')}));
		return genesisObj;
	},
	
	/****************************************************
	* FUNCTION 
	*		saveBookContent
	*
	* DESCRIPTION
	* 		Create String Book Content from Dom.
	*
	* ARGUMENTS
	*		bookDom	:	Content Dom
	*
	* RETURN
	*		bookxmlDom
	*
	****************************************************/
	saveBookContent : function(bookDom)
	{
		var genesisObj = new Element('Content',{});
		var style      = new Element('PropertyStyle',{});
		genesisObj.appendChild(style);
		style.appendChild(new Element("left"   ,{'value':bookDom.style.left}));
		style.appendChild(new Element("top"    ,{'value':bookDom.style.top}));
		style.appendChild(new Element("width"  ,{'value':bookDom.style.width}));
		style.appendChild(new Element("height" ,{'value':bookDom.style.height}));
		var innerhtml  = new Element('innerHTML',{'Type':'html'});
		genesisObj.appendChild(innerhtml);
		innerhtml.value = '<![CDATA['+bookDom.innerHTML+']]>';
		return genesisObj;
	},
	
	/****************************************************
	* FUNCTION 
	*		saveBookExternalEmbeded
	*
	* DESCRIPTION
	* 		Create String Book ExternalEmbeded from Dom.
	*
	* ARGUMENTS
	*		bookDom	:	ExternalEmbeded Dom
	*
	* RETURN
	*		bookxmlDom
	*
	****************************************************/
	saveBookExternalEmbeded : function(bookDom)
	{
		var genesisObj = new Element('ExternalEmbeded',{});
		var style      = new Element('PropertyStyle',{});
		genesisObj.appendChild(style);
		style.appendChild(new Element("left"   ,{'value':bookDom.style.left}));
		style.appendChild(new Element("top"    ,{'value':bookDom.style.top}));
		style.appendChild(new Element("width"  ,{'value':bookDom.style.width}));
		style.appendChild(new Element("height" ,{'value':bookDom.style.height}));
		var innerhtml  = new Element('innerHTML',{'Type':'html'});
		genesisObj.appendChild(innerhtml);
		innerhtml.value = '<![CDATA['+bookDom.innerHTML+']]>';
		return genesisObj;
	},
	
	/****************************************************
	* FUNCTION 
	*		saveBookGenesis
	*
	* DESCRIPTION
	* 		Create String Book Genesis from Dom.
	*
	* ARGUMENTS
	*		bookDom	:	Content Dom
	*
	* RETURN
	*		bookxmlDom
	*
	****************************************************/
	saveBookGenesis : function(bookDom)
	{
		var genesisObj = new Element('Genesis',{});
		var style      = new Element('PropertyStyle',{});
		genesisObj.appendChild(style);
		style.appendChild(new Element("left"   ,{'value':bookDom.style.left}));
		style.appendChild(new Element("top"    ,{'value':bookDom.style.top}));
		style.appendChild(new Element("width"  ,{'value':bookDom.style.width}));
		style.appendChild(new Element("height" ,{'value':bookDom.style.height}));
		var childnode  = bookDom.getElementsByTagName('childnode');
		for(var i =0;i<childnode.length;i++)
		{
			var node = childnode[i];
			if("BookImage" == node.getDomType())
			{
				genesisObj.appendChild(this.saveBookImage(node.getDomObj()));
			}
			else if("Content" == node.getDomType())
			{
				genesisObj.appendChild(this.saveBookContent(node.getDomObj()));
			}
			else if("ExternalEmbeded" == node.getDomType())
			{
				genesisObj.appendChild(this.saveBookExternalEmbeded(node.getDomObj()));
			}
		}
		return genesisObj;
	}
};